//封装与后端的请求
import { ServerConfig, EleServerURL } from '@util/serverconfig';
import { Local } from '@/store/cache';
import axios from 'axios';

export const RequestUtil = {
	save_user_token: token => {
		ServerConfig.saveEleServerUserToken(token);
	},
	get_user_token: () => {
		let token = Local.get(EleServerURL.EleServerUserTokenKey);
		if (!token && token != '') {
			return token;
		}
		return null;
	},
	do_get: (url, paramObj, doSuccess, doError, hasReturn = false) => {
		let eleServer = ServerConfig.getEleServer();
		if (paramObj) {
			const paramData = {};
			paramData.params = paramObj;
			eleServer
				.get(url, paramData)
				.then(response => {
					if (true == hasReturn) {
						return doSuccess(response);
					} else {
						doSuccess(response);
					}
				})
				.catch(error => {
					doError(error);
				});
		} else {
			eleServer
				.get(url)
				.then(response => {
					if (true == hasReturn) {
						return doSuccess(response);
					} else {
						doSuccess(response);
					}
				})
				.catch(error => {
					doError(error);
				});
		}
	},
	do_put: (url, paramObj, doSuccess, doError, hasReturn = false) => {
		let eleServer = ServerConfig.getEleServer();
		if (paramObj) {
			// const paramData = {}
			// paramData.params = paramObj
			eleServer
				.put(url, paramObj)
				.then(response => {
					if (true == hasReturn) {
						return doSuccess(response);
					} else {
						doSuccess(response);
					}
				})
				.catch(error => {
					doError(error);
				});
		} else {
			eleServer
				.put(url)
				.then(response => {
					if (true == hasReturn) {
						return doSuccess(response);
					} else {
						doSuccess(response);
					}
				})
				.catch(error => {
					doError(error);
				});
		}
	},
	do_post: (url, paramData, doSuccess, doError, hasReturn = false) => {
		let eleServer = ServerConfig.getEleServer();
		eleServer
			.post(url, paramData)
			.then(response => {
				if (true == hasReturn) {
					return doSuccess(response);
				} else {
					doSuccess(response);
				}
			})
			.catch(error => {
				doError(error);
			});
	},
	do_get_async: async (url, paramObj, doSuccess, doError, hasReturn = false) => {
		let eleServer = ServerConfig.getEleServer();
		try {
			if (paramObj) {
				const paramData = {};
				paramData.params = paramObj;
				const response = await eleServer.get(url, paramData);
				if (true == hasReturn) {
					return doSuccess(response);
				} else {
					doSuccess(response);
				}
			} else {
				const response = eleServer.get(url);
				if (true == hasReturn) {
					return doSuccess(response);
				} else {
					doSuccess(response);
				}
			}
		} catch (error) {
			doError(error);
		}
	},
	do_post_async: async (url, paramObj, doSuccess, doError, hasReturn = false) => {
		let eleServer = ServerConfig.getEleServer();
		try {
			const paramData = {};
			paramData.params = paramObj;
			const response = await eleServer.post(url, paramData);
			if (true == hasReturn) {
				return doSuccess(response);
			} else {
				doSuccess(response);
			}
		} catch (error) {
			doError(error);
		}
	},
	do_delete: (url, paramData, doSuccess, doError, hasReturn = false) => {
		let eleServer = ServerConfig.getEleServer();
		eleServer
			.delete(url, paramData)
			.then(response => {
				if (true == hasReturn) {
					return doSuccess(response);
				} else {
					doSuccess(response);
				}
			})
			.catch(error => {
				doError(error);
			});
	},
	do_post_upload_async: async (url, formDataObj, doSuccess, doError, hasReturn = false) => {
		let eleServer = ServerConfig.getEleUploadServer();
		try {
			const response = await eleServer.post(url, formDataObj);
			if (true == hasReturn) {
				return doSuccess(response);
			} else {
				doSuccess(response);
			}
		} catch (error) {
			doError(error);
		}
	},
	do_file_download:  (url, fileName, fileSuffix) => {
		//通过blob对象下载，隐藏下载地址
		fetch(EleServerURL.url + url)
			.then(res => res.blob())
			.then(blob => {
				let a = document.createElement('a');
				a.setAttribute('href', URL.createObjectURL(blob));
				a.setAttribute('target', 'win_download');
				a.setAttribute('download', fileName);
				document.body.appendChild(a);
				a.click();
				document.body.removeChild(a);
		 	URL.revokeObjectURL(a.href); // 释放URL 对象
			});
	},
	do_downloadfile_withToken:  (url, fileName, fileSuffix,method="POST") => {
		let token = Local.get(EleServerURL.EleServerUserTokenKey);
		let _header= new Headers();
		_header.append("Authorization", token);
		_header.append("Accept", "*/*");
		_header.append("Connection", "keep-alive");
		let _url=EleServerURL.url + url;
		//通过blob对象下载，隐藏下载地址
		fetch(_url,{method: method,
		   headers: _header,
		   redirect: 'follow'})
			.then(res => res.blob())
			.then(blob => {
				let a = document.createElement('a');
				a.setAttribute('href', URL.createObjectURL(blob));
				a.setAttribute('target', 'win_download');
				a.setAttribute('download', fileName);
				document.body.appendChild(a);
				a.click();
				document.body.removeChild(a);
		 	URL.revokeObjectURL(a.href); // 释放URL 对象
			});
	}
};
